package educrypt.gui.components;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionListener;

import educrypt.commons.Global;
import educrypt.gui.view.EditorKeywordView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

class EditorKeywordDialog extends EducryptDialog implements EditorKeywordView {

	private static final long serialVersionUID = 6343705910433156181L;

	private final JPanel contentPanel = new JPanel();
	private JTextField tfSelectedKeyword;
	private JList lstKeyword;
	private JComboBox cbxAlphabet;
	private JButton btnValidateKeyword;
	private JButton btnAddKeyword;
	private JButton btnRemoveKeyword;
	private JScrollPane scrollPane;
	private JComboBox cbxCipherType;

	public EditorKeywordDialog(Window owner) {
		super(owner);

		setTitle(Messages.getString("guiKeywordEditor"));
		setBounds(100, 100, 460, 360);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblAlphabet = new JLabel(Messages.getString("guiAlphabet"));
		lblAlphabet.setBounds(21, 88, 111, 14);
		contentPanel.add(lblAlphabet);

		cbxAlphabet = new JComboBox();
		cbxAlphabet.setBounds(142, 85, 234, 20);
		contentPanel.add(cbxAlphabet);

		tfSelectedKeyword = new JTextField();
		tfSelectedKeyword.setBounds(142, 120, 234, 24);
		contentPanel.add(tfSelectedKeyword);
		tfSelectedKeyword.setColumns(10);

		ImageIcon iconAdd = new ImageIcon(
				EduCryptResources.getImageURL("plus24.png"));
		ImageIcon iconValidate = new ImageIcon(
				EduCryptResources.getImageURL("validate24.png"));
		ImageIcon iconRemove = new ImageIcon(
				EduCryptResources.getImageURL("remove24color.png"));

		btnValidateKeyword = new JButton();
		btnValidateKeyword.setIcon(iconValidate);
		btnValidateKeyword.setBounds(386, 120, 24, 24);
		btnValidateKeyword.setIcon(iconValidate);
		contentPanel.add(btnValidateKeyword);

		btnAddKeyword = new JButton();
		btnAddKeyword.setBounds(418, 120, 24, 24);
		btnAddKeyword.setIcon(iconAdd);
		contentPanel.add(btnAddKeyword);

		btnRemoveKeyword = new JButton();
		btnRemoveKeyword.setBounds(418, 160, 24, 24);
		btnRemoveKeyword.setIcon(iconRemove);
		contentPanel.add(btnRemoveKeyword);

		lstKeyword = new JList();
		scrollPane = new JScrollPane(lstKeyword);
		scrollPane.setBounds(142, 160, 268, 150);
		contentPanel.add(scrollPane);

		JLabel lblVigenereKeywordEditor = new JLabel(
				Messages.getString("guiKeywordEditor"));
		lblVigenereKeywordEditor.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblVigenereKeywordEditor.setBounds(21, 11, 378, 24);
		contentPanel.add(lblVigenereKeywordEditor);

		JLabel lblNewKeyword = new JLabel(Messages.getString("guiNewKeyword"));
		lblNewKeyword.setBounds(21, 125, 111, 14);
		contentPanel.add(lblNewKeyword);

		JLabel lblStoredKeywords = new JLabel(
				Messages.getString("guiStoredKeywords"));
		lblStoredKeywords.setBounds(21, 160, 111, 28);
		contentPanel.add(lblStoredKeywords);

		JLabel lblCipher = new JLabel(Messages.getString("guiCipherType"));
		lblCipher.setBounds(21, 51, 111, 14);
		contentPanel.add(lblCipher);

		cbxCipherType = new JComboBox();
		cbxCipherType.setBounds(142, 48, 234, 20);
		contentPanel.add(cbxCipherType);

		showCiphers();
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnAddKeyword.addActionListener((ActionListener) ctr);
		btnAddKeyword.setActionCommand(ADD_KEYWORD);

		btnRemoveKeyword.addActionListener((ActionListener) ctr);
		btnRemoveKeyword.setActionCommand(REMOVE_KEYWORD);

		btnValidateKeyword.addActionListener((ActionListener) ctr);
		btnValidateKeyword.setActionCommand(VALIDATE_KEYWORD);

		cbxCipherType.addActionListener((ActionListener) ctr);
		cbxCipherType.setActionCommand(CIPHER_SELECTED);

		cbxAlphabet.addActionListener((ActionListener) ctr);
		cbxAlphabet.setActionCommand(ALPHABET_SELECTED);

		lstKeyword.addListSelectionListener((ListSelectionListener) ctr);

		tfSelectedKeyword.getDocument().addDocumentListener(
				new DocumentListener() {
					@Override
					public void removeUpdate(DocumentEvent arg0) {
						btnAddKeyword.setEnabled(false);
					}

					@Override
					public void insertUpdate(DocumentEvent arg0) {
						btnAddKeyword.setEnabled(false);
					}

					@Override
					public void changedUpdate(DocumentEvent arg0) {
						btnAddKeyword.setEnabled(false);
					}
				});
	}

	public void showCiphers() {
		String[] ciphers = new String[] { Messages.getString("cipherVigenere"),
				Messages.getString("cipherPlayfair"),
				Messages.getString("cipherHill") };
		cbxCipherType.setModel(new DefaultComboBoxModel(ciphers));
	}

	@Override
	public void showAlphabets(List<String> alphabets) {
		cbxAlphabet.setModel(new DefaultComboBoxModel(alphabets.toArray()));
	}

	@Override
	public void hideAlphabets() {
		if (cbxAlphabet.isEnabled()) {
			assert cbxAlphabet.getModel().getSize() > 0;

			cbxAlphabet.setSelectedIndex(0);
			cbxAlphabet.setEnabled(false);
		}
	}

	@Override
	public void showKeywords(List<String> keywords) {
		lstKeyword.setEnabled(true);
		tfSelectedKeyword.setEnabled(true);
		lstKeyword.setListData(keywords.toArray());
	}

	@Override
	// Remove items from list and disable list and textarea
	public void hideKeywords() {
		lstKeyword.setEnabled(false);
		tfSelectedKeyword.setText("");
		tfSelectedKeyword.setEnabled(false);
	}

	@Override
	public int setSelectedCipher(int cipher) {
		int index = 0;
		switch (cipher) {
		case Global.VIGENERE:
			index = 0;
			break;
		case Global.PLAYFAIR:
			index = 1;
			break;
		case Global.HILL:
			index = 2;
			break;
		}
		cbxCipherType.setSelectedIndex(index);
		return index;
	}

	@Override
	public int getSelectedCipher() {
		int selectedCipher = 0;
		int selectedIndex = cbxCipherType.getSelectedIndex();
		switch (selectedIndex) {
		case 0:
			selectedCipher = Global.VIGENERE;
			break;
		case 1:
			selectedCipher = Global.PLAYFAIR;
			break;
		case 2:
			selectedCipher = Global.HILL;
			break;
		}
		return selectedCipher;
	}

	@Override
	public void setSelectedAlphabetName(String alphabetName) {
		cbxAlphabet.setSelectedItem(alphabetName);
	}

	@Override
	public String getSelectedAlphabetName() {
		Object selectedObject = cbxAlphabet.getSelectedItem();
		if (selectedObject != null) {
			return (String) selectedObject;
		} else {
			return "";
		}
	}

	@Override
	public String getSelectedKeyword() {
		Object selectedValue = lstKeyword.getSelectedValue();
		String selectedKeyword = (selectedValue != null) ? selectedValue
				.toString() : "";
		return selectedKeyword;
	}

	@Override
	public void setKeywordToEdit(String keyword) {
		this.tfSelectedKeyword.setText(keyword);
	}

	@Override
	public String getKeywordToEdit() {
		return this.tfSelectedKeyword.getText();
	}

	@Override
	public void enableButtons(boolean show) {
		btnValidateKeyword.setEnabled(show);
		btnAddKeyword.setEnabled(show);
		btnRemoveKeyword.setEnabled(show);
	}

	@Override
	public void enableAddButton(boolean show) {
		btnAddKeyword.setEnabled(show);
	}
}
